/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cognition.util.j2me;

import java.util.Hashtable;

/**
 * This is a thread-consciencous logger that can be use on small devices.
 * It employs a single threaded model and allows for different logging levels.
 * Things can be logged using tags.
 * @author richard
 */
public class SystemLogger {

    /**
     * Default logger for console output.
     */
    public static final String LOGGER_CONSOLE = 
            "com.cognition.util.j2me.LoggerConsole";

    /**
     * Default logger for capturing events.
     */
    public static final String LOGGER_EVENTS =
            "com.cognition.util.j2me.LoggerEvents";

    public static final int LOG_LEVEL_DEBUG = 0;
    public static final int LOG_LEVEL_INFO = 1;
    public static final int LOG_LEVEL_WARN = 2;
    public static final int LOG_LEVEL_ERROR = 3;
    public static final int LOG_LEVEL_CRITICAL = 4;

    public static final String[] LOG_LEVELS = {
        "DEBUG",
        "INFO",
        "WARN",
        "ERROR",
        "CRITICAL"
    };

    private static int _level = LOG_LEVEL_CRITICAL;

    private static Hashtable loggers = new Hashtable();

    /**
     * Sets the current logging level.  One of the values beginning with
     * LOG_LEVEL (constants) must be passed to this method.
     * @param l the logging level to use.
     */
    public static void setLevel(int l){
        if (!isValidLevel(l))
            throw new IllegalArgumentException("invalid debug level.");
        _level = l;
    }

    public static boolean isValidLevel(int l){
        
        return (l < 0 || l >= LOG_LEVELS.length) ? false : true;

    }

    public static int getCurrentLevel(){
        return _level;
    }
    
    public static String getLevelName(int l){
        if (!isValidLevel(l))
            return null;

        return LOG_LEVELS[l];
    }


    private SystemLogger(){}

    /**
     * Returns the logger that is implemented with the specified class name.
     * Null is returned if the logger cannot be found.
     * @param className the name of the class that implements the Logger class.
     * @return the logger class, or null if the class could not be found.
     */
    public synchronized static Logger getLogger(String className) {
        Logger log = (Logger) loggers.get(className);

        if (log != null)
            return log;

        
        // try to instantiate the class
        try {
            Class clss = Class.forName(className);
            Object obj = clss.newInstance();
            if (!(obj instanceof Logger)) 
                throw new IllegalArgumentException("class name does not extend Logger");

            log = (Logger)obj;
    
            loggers.put(className, log);
            return log;
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return null;
    }

}
